﻿Imports System.IO
Imports System.Xml

''' <summary>
''' Hulpklasse voor applicatiesettings.
''' Deze worden in een eigen xml-bestand opgeslagen.
''' Bij de standaard applicatiesettings zijn deze read-only, daarom is deze werkwijze nodig.
''' Voor usersettings is er geen probleem en kan My.Settings gebruikt worden.
''' </summary>
Public Class Configuratie

#Region "Property's"

    Public Shared Property Printer() As String
        Get
            Return GetSetting("printers/printer", "")
        End Get
        Set(ByVal value As String)
            SetSetting("printers/printer", value)
        End Set
    End Property

    Public Shared Property TicketPrinter() As String
        Get
            Return GetSetting("printers/ticketprinter", "")
        End Get
        Set(ByVal value As String)
            SetSetting("printers/ticketprinter", value)
        End Set
    End Property

    Public Shared Property TicketAfdrukken() As Boolean
        Get
            Return GetSetting("ticketafdrukken", "ja") <> "nee" ' ook bij lege string is het True
        End Get
        Set(ByVal value As Boolean)
            Dim strValue As String = "nee"
            If (value) Then strValue = "ja"
            SetSetting("ticketafdrukken", strValue)
        End Set
    End Property

    Public Shared Property KasBladAfdrukken() As Boolean
        Get
            Return GetSetting("kasbladafdrukken", "ja") = "ja"
        End Get
        Set(ByVal value As Boolean)
            Dim strValue As String = "nee"
            If (value) Then strValue = "ja"
            SetSetting("kasbladafdrukken", strValue)
        End Set
    End Property

    Public Shared Property LeveringsbonAfdrukken() As Boolean
        Get
            Return GetSetting("leveringsbonafdrukken", "ja") = "ja"
        End Get
        Set(ByVal value As Boolean)
            Dim strValue As String = "nee"
            If (value) Then strValue = "ja"
            SetSetting("leveringsbonafdrukken", strValue)
        End Set
    End Property

    Public Shared Property TekstOpTicket() As String
        Get
            Return GetSetting("tekstopticket", "")
        End Get
        Set(ByVal value As String)
            SetSetting("tekstopticket", value)
        End Set
    End Property

    Public Shared Property ToonMonitor() As Boolean
        Get
            Return GetSetting("toonmonitor", "nee") = "ja"
        End Get
        Set(ByVal value As Boolean)
            Dim strValue As String = "nee"
            If (value) Then strValue = "ja"
            SetSetting("toonmonitor", strValue)
        End Set
    End Property

#End Region

#Region "Constructor/initialisatie"

    Private Sub New() ' zorg dat er geen publieke constructor is
    End Sub

#End Region

#Region "Private methods"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="naamNode">pad van nodes, gescheiden door /</param>
    ''' <returns></returns>
    Private Shared Function GetSetting(ByVal naamNode As String, ByVal defaultValue As String) As String

        Dim settingsFilename As String = GetSettingsFileName()
        If (Not File.Exists(settingsFilename)) Then Return ""

        'Create the XML Document
        Dim m_xmld As XmlDocument = New XmlDocument()

        'Load the Xml file
        m_xmld.Load(settingsFilename)

        'Get the list of name nodes 
        Dim m_node As XmlNode = m_xmld.SelectSingleNode("/configuratie/" & naamNode)
        If (m_node IsNot Nothing) Then
            Return m_node.InnerText
        End If
        Return defaultValue

    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="naamNode">pad van nodes, gescheiden door /</param>
    ''' <param name="value"></param>
    ''' <remarks></remarks>
    Private Shared Sub SetSetting(ByVal naamNode As String, ByVal value As String)
        Try
            Dim settingsFilename As String = GetSettingsFileName()

            If (Not File.Exists(settingsFilename)) Then
                'Create the XML Document
                Dim xml As String = My.Resources.configuratie
                File.WriteAllText(settingsFilename, xml)
            End If

            Dim m_xmld As New XmlDocument()
            'Load the Xml file
            m_xmld.Load(settingsFilename)

            'Get the node 
            Dim m_node As XmlNode = m_xmld.SelectSingleNode("/configuratie/" & naamNode)
            If (m_node Is Nothing) Then
                ' instelling bestond nog niet
                ' voeg de instelling toe aan de parentnode
                m_node = m_xmld.SelectSingleNode("/configuratie")
                Dim aanTeMakenNodes() As String = naamNode.Split("/"c)
                ' maak de nodes recursief aan
                For Each aanTeMakenNode As String In aanTeMakenNodes
                    Dim testnode As XmlNode = m_node.SelectSingleNode(aanTeMakenNode)
                    If (testnode IsNot Nothing) Then
                        ' node bestond al
                        m_node = testnode
                    Else
                        ' node bestond nog niet
                        Dim newnode As XmlElement = m_xmld.CreateElement(aanTeMakenNode)
                        m_node.AppendChild(newnode)
                        m_node = newnode
                    End If
                Next
            End If
            m_node.InnerText = value

            m_xmld.Save(settingsFilename)

        Catch ex As Exception
            'Error trapping
            log.WriteError("Configuratie.SetSetting", ex)
            MsgBox.ShowError(ex.ToString)
        End Try
    End Sub

    Private Shared Function GetSettingsFileName() As String
        Dim dir As New DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory)
        Dim settingsFileName As String = Path.Combine(dir.FullName, "configuratie.xml")
        Return settingsFileName
    End Function

#End Region

End Class
